package com.yunsc.order.handler.exception;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 异常处理切面
 * @date: 2024-06-13 21:56
 */
@Slf4j
@Aspect
@Component
public class DelayExceptionHandlingAspect {

    @AfterThrowing(pointcut = "@annotation(DelayExceptionHandler)")
    public void handleExceptionAndLog(JoinPoint joinPoint)  throws Throwable {
        // 统一异常处理逻辑
        log.error("延迟队列处理订单信息异常: " );
        // 获取方法参数中的订单号
        String orderId = extractOrderId(joinPoint.getArgs());
        String remark = extractDescription(joinPoint.getArgs());
        //  记录日志
        log.error("订单号: {}, 业务描述: {}", orderId, remark);
    }

    // 从方法参数中提取订单号，订单号在Map的"orderId"键中
    private String extractOrderId(Object[] args) {
        if (args != null && args.length > 0 && args[0] instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) args[0];
            return (String) map.get("orderId");
        }
        return null;
    }

    // 从方法参数中提取业务执行描述，假设描述在Map的"remark"键中
    private String extractDescription(Object[] args) {
        if (args != null && args.length > 0 && args[0] instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) args[0];
            return (String) map.get("remark");
        }
        return null;
    }

}
